//
//  HLIMKitSessionContentConfigFactory.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLIMKitSessionContentConfigFactory.h"
#import "HLIMKitTextMessageContentConfig.h"
#import "HLIMKitImageContentConfig.h"
#import "HLIMKitAudioContentConfig.h"
#import "HLIMKitVideoContentConfig.h"
#import "HLIMKitNotificationContentConfig.h"
#import "HLIMKitTipContentConfig.h"
#import "HLIMKitVideoChatContentConfig.h"
#import "HLIMKitUndefineContentConfig.h"

@implementation HLIMKitSessionContentConfigFactory
{
    NSDictionary *_dict;
    NSDictionary *_replyDict;
    HLIMKitUndefineContentConfig *_undefineConfig;
}

+ (instancetype)sharedFacotry
{
    static HLIMKitSessionContentConfigFactory *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });

    return _sharedManager;
}

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        /**
         * _dict 内包含的为支持的类型
         * 通过删减可以控制所支持的消息类型
         */
        _dict =@{
            @(NIMMessageTypeText)         :       [HLIMKitTextMessageContentConfig new],
            @(NIMMessageTypeImage)        :       [HLIMKitImageContentConfig new],
            @(NIMMessageTypeAudio)        :       [HLIMKitAudioContentConfig new],
            @(NIMMessageTypeVideo)        :       [HLIMKitVideoContentConfig new],
            @(NIMMessageTypeRtcCallRecord)    :    [HLIMKitVideoChatContentConfig new],
            @(NIMMessageTypeTip)        :    [HLIMKitTipContentConfig new]
        };
        _undefineConfig = [HLIMKitUndefineContentConfig.alloc init];
    }
    
    return self;
}

- (id<HLIMSessionContentConfig>)configBy:(NIMMessage *)message
{
    NIMMessageType messageType = message.messageType;
    id<HLIMSessionContentConfig>config = [_dict objectForKey:@(messageType)];
    if (config == nil) {
        // 不支持的消息类型
        if (messageType == NIMMessageTypeNotification) {
            NIMNotificationObject *object = (NIMNotificationObject *)message.messageObject;
            if (object.notificationType == NIMNotificationTypeNetCall) {
                return [_dict objectForKey:@(NIMNotificationTypeNetCall+20)];
            }
        }
        config = _undefineConfig;
    }
    return config;
}

- (id<HLIMSessionContentConfig>)replyConfigBy:(NIMMessage *)message
{
    NIMMessageType type = message.messageType;
    id<HLIMSessionContentConfig>config = [_replyDict objectForKey:@(type)];
    if (config == nil)
    {
        config = _undefineConfig;
    }
    return config;
}


@end
